package com.example.mntz.controller;

import com.example.mntz.model.Account;
import com.example.mntz.model.Classs;
import com.example.mntz.model.User;
import com.example.mntz.service.AccService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/acc")
public class AccController {

    @Autowired
    AccService service;
    @RequestMapping("/acchtml")
    public String acchtml(){
        return "account-myacc";
    }
    /*查找记账本标签*/
    @ResponseBody
    @RequestMapping("/selectbq")
    public List<Classs> selectbq(HttpSession session){
        User user = (User)session.getAttribute("session_user");
        List<Classs> classes = service.selectbq(user.getuId());
        return classes;
    }

    /*修改标签页面*/
    @RequestMapping("/bjXg")
    public ModelAndView bjPost(HttpSession session, Integer classId ){
        ModelAndView mav = new ModelAndView("account-accClassSq");
        User user = (User)session.getAttribute("session_user");
        Classs cl = new Classs();
        if(classId != null){
            cl= service.selectbq(user.getuId(),classId);
        }
        mav.addObject("class",cl);
        return mav;
    }
    /*修改标签*/
    @ResponseBody
    @RequestMapping("/xgAcc")
    public String xgAcc(Classs claess ){
        service.insertClass(claess);
        return "成功";
    }
    /*查找记账记录*/
    @ResponseBody
    @RequestMapping("/selectaccs")
    public List<Account> selectaccs(String accountTime,HttpSession session){
        if (accountTime == null){
            accountTime = (new Date()).toString();
        }
        Integer uId = ((User)session.getAttribute("session_user")).getuId();
        List<Account> accounts =  service.selectaccounts(accountTime,uId);
        return accounts;
    }
    /*添加记账记录*/
    @RequestMapping("/tjacc")
    public ModelAndView tjacc(Integer accountId){
        ModelAndView mav = new ModelAndView("account-addAcc");
        Account account = new Account();
        if(accountId != null ){
            account = service.selectacc(accountId);
        }
        mav.addObject("account",account);
        return mav;
    }
    /*查找记账本标签*/
    @ResponseBody
    @GetMapping("/selectbqs")
    public Map selectbqs(HttpSession session,String id){
        User user = (User)session.getAttribute("session_user");
        List<Classs> classes = service.selectbq(user.getuId());
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","success");
        map.put("data",classes);
        return map;
    }

    /*添加记录*/
    @ResponseBody
    @PostMapping("/addacc")
    public void addacc(Account account){
        service.insertAccount(account);
    }

    @RequestMapping("/accBbhtml")
    public String accBbhtml(){
        return "account-myBb";
    }

    @ResponseBody
    @GetMapping("/tbsx")
    public List<Map>  tbsx(String time,String  redio,HttpSession session){
        Integer uId = ((User)session.getAttribute("session_user")).getuId();
        return service.selecthPieClass(time,redio,uId);
    }


}
